/**
 * $Revision: 1.0 $
 * $Author: Geln Yang $
 * $Date: Jun 16, 2010 4:53:37 PM $
 *
 * Author: Geln Yang
 * Date  : Jun 16, 2010 4:53:37 PM
 *
 */
package com.gooex.service.common.impl;

import java.util.List;
import org.c4j.system.web.hibernate.BaseHibernateService;
import org.c4j.system.web.page.Page;

import com.gooex.domain.common.AppMessage;
import com.gooex.service.common.IAppMessageManager;

/**
 * @author Geln Yang
 * @version 1.0
 */
public class AppMessageManagerImpl extends BaseHibernateService implements IAppMessageManager {

	public AppMessage getMessage(String id) {
		return (AppMessage) getHibernateTemplate().get(AppMessage.class, id);
	}

	public List<AppMessage> getMessageByFrom(String from, Page page) {
		return findHQLQuery("from " + AppMessage.class.getName() + " where from=?", new Object[] { from }, page);
	}

	public List<AppMessage> getMessageByTo(String to, Page page) {
		return findHQLQuery("from " + AppMessage.class.getName() + " where to=?", new Object[] { to }, page);
	}

	public void removeMessage(String id) {
		AppMessage message = getMessage(id);
		if (message != null)
			getHibernateTemplate().delete(message);
	}

	public void saveMessage(AppMessage message) {
		getHibernateTemplate().saveOrUpdate(message);
	}

	public int getUnreadMsgCount(String to) {
		String queryString = "select count(*) from " + AppMessage.class.getName() + " where to=? and read=0";
		Object[] params = new Object[] { to };
		return findHQLQueryCount(queryString, params);
	}

}
